package com.cmc.cloud.cmclink.doc.util;

import java.util.ArrayList;
import java.util.List;

import com.cmes.framework.common.exception.ErrorCode;
import com.cmes.framework.common.exception.ServiceException;

/**
 * @author: dev89
 * @date: 2025-10-28
 */
public class ExceptionUtil {

    public static ServiceException exception(ErrorCode errorCode, Object... params) {
        String[] strParams = toStringArray(params);
        return new ServiceException(errorCode.getCode(), errorCode.getMsg(), strParams);
    }

    private static String[] toStringArray(Object... params) {
        List<String> strParams = new ArrayList<>();

        for (Object param : params) {
            if (param == null) {
                strParams.add("null");
            } else {
                strParams.add(param.toString());
            }
        }

        return strParams.toArray(new String[strParams.size()]);
    }

    public static ServiceException exception(ErrorCode errorCode) {
        return new ServiceException(errorCode.getCode(), errorCode.getMsg());
    }

}
